package com.ruoyi.gqgl.psb.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.gqgl.psb.domain.Psb;
import com.ruoyi.gqgl.psb.mapper.PsbMapper;
import com.ruoyi.gqgl.psb.service.IPsbService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

import static com.ruoyi.common.utils.SecurityUtils.*;

/**
 * PSService业务层处理
 *
 * @author huacai
 * @date 2025-01-20
 */
@Service
public class PsbServiceImpl implements IPsbService {
    @Resource
    private PsbMapper psbMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询PS
     *
     * @param psbId PS主键
     * @return PS
     */
    @Override
    public Psb selectPsbByPsbId(String psbId) {
        return psbMapper.selectPsbByPsbId(psbId);
    }

    /**
     * 查询PS列表
     *
     * @param psb PS
     * @return PS
     */
    @Override
    public List<Psb> selectPsbList(Psb psb) {
        return psbMapper.selectPsbList(psb);
    }

    /**
     * 新增PS
     *
     * @param psb PS
     * @return 结果
     */
    @Override
    public int insertPsb(Psb psb) {
        psb.setCreateTime(DateUtils.getNowDate());
        psb.setPsbId(IdUtils.fastSimpleUUID());
        psb.setUserId(Math.toIntExact(getUserId())); //用户ID
        psb.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        psb.setCreateBy(getUsername()); //创建人
        psb.setIdWhere("存在");
        return psbMapper.insertPsb(psb);
    }

    /**
     * 批量新增PS
     *
     * @param psbs PSList
     * @return 结果
     */
    @Override
    public int batchInsertPsb(List<Psb> psbs) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(psbs)) {
            try {
                for (int i = 0; i < psbs.size(); i++) {
                    int row = psbMapper.insertPsb(psbs.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == psbs.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改PS
     *
     * @param psb PS
     * @return 结果
     */
    @Override
    public int updatePsb(Psb psb) {
        psb.setUpdateTime(DateUtils.getNowDate());
        return psbMapper.updatePsb(psb);
    }

    /**
     * 批量删除PS
     *
     * @param psbIds 需要删除的PS主键
     * @return 结果
     */
    @Override
    public int deletePsbByPsbIds(String[] psbIds) {
        return psbMapper.deletePsbByPsbIds(psbIds);
    }

    /**
     * 删除PS信息
     *
     * @param psbId PS主键
     * @return 结果
     */
    @Override
    public int deletePsbByPsbId(String psbId) {
        return psbMapper.deletePsbByPsbId(psbId);
    }

    /**
     * 根据认定年度不分页查询PS表
     * @param rdNd
     * @return
     */
    @Override
    public List<Psb> selectPSbByRdNd(String rdNd) {
        return psbMapper.selectPSbByRdNd(rdNd);
    }
}
